package cn.yuanqiao.web.controller.archive.bianyan;

import cn.yuanqiao.archive.bianyan.domain.YqZhuantiChengguo;
import cn.yuanqiao.archive.bianyan.domain.YqZtcgFile;
import cn.yuanqiao.archive.bianyan.service.IYqZhuantiChengguoService;
import cn.yuanqiao.common.core.domain.R;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 专题成果
 *
 * @ProjectName: ruoyi-modules-system
 * @Package: cn.yuanqiao.web.controller.archive.bianyan
 * @ClassName: YqZhuantiChengguoController
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2023/7/11 11:14
 * @Version: 1.0
 */
@RestController
@RequestMapping("/archive/zt/achievement")
public class YqZhuantiChengguoController extends BaseController {
    @Autowired
    private IYqZhuantiChengguoService yqZhuantiChengguoService;

    /**
     * 查询专题成果列表
     */
    @PreAuthorize("@ss.hasPermi('archive:zt:achievement:query')")
    @GetMapping("/list")
    public TableDataInfo list(YqZhuantiChengguo yqZhuantiChengguo) {
        startPage();
        List<YqZhuantiChengguo> list = yqZhuantiChengguoService.selectYqZhuantiChengguoList(yqZhuantiChengguo);
        return getDataTable(list);
    }

    /**
     * 根据成果id查询附件信息
     *
     * @param cgId
     * @return
     */
    @PostMapping("/file/list")
    public TableDataInfo list(@RequestParam("cgId") String cgId) {
        startPage();
        List<YqZtcgFile> list = yqZhuantiChengguoService.selectztcgFileChengguoId(cgId);
        return getDataTable(list);
    }

    /**
     * 导出专题成果列表
     */
    @PreAuthorize("@ss.hasPermi('archive:zt:achievement:export')")
    @Log(title = "专题成果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqZhuantiChengguo yqZhuantiChengguo) {
        List<YqZhuantiChengguo> list = yqZhuantiChengguoService.selectYqZhuantiChengguoList(yqZhuantiChengguo);
        ExcelUtil<YqZhuantiChengguo> util = new ExcelUtil<YqZhuantiChengguo>(YqZhuantiChengguo.class);
        util.exportExcel(response, list, "专题成果数据");
    }

    /**
     * 获取专题成果详细信息
     */
    @GetMapping(value = "/{chengguoId}")
    public AjaxResult getInfo(@PathVariable("chengguoId") String chengguoId) {
        return success(yqZhuantiChengguoService.selectYqZhuantiChengguoByChengguoId(chengguoId));
    }

    /**
     * 新增专题成果
     */
    @PreAuthorize("@ss.hasPermi('archive:zt:achievement:add')")
    @Log(title = "专题成果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqZhuantiChengguo yqZhuantiChengguo) {
        return toAjax(yqZhuantiChengguoService.insertYqZhuantiChengguo(yqZhuantiChengguo));
    }

    /**
     * 文件上传请求
     */
    @PostMapping("/upload")
    R<YqZtcgFile> uploadPicture(MultipartFile file, @RequestParam("id") String id) {
        return yqZhuantiChengguoService.uploadFile(file, id);
    }

    /**
     * 删除上传文件
     *
     * @param fileId
     * @return
     */
    @PostMapping("/file/remove")
    public AjaxResult fileRemove(@RequestParam("fileId") String fileId) {
        return toAjax(yqZhuantiChengguoService.fileDelete(fileId));
    }

    /**
     * 修改专题成果
     */
    @PreAuthorize("@ss.hasPermi('archive:zt:achievement:edit')")
    @Log(title = "专题成果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqZhuantiChengguo yqZhuantiChengguo) {
        return toAjax(yqZhuantiChengguoService.updateYqZhuantiChengguo(yqZhuantiChengguo));
    }

    /**
     * 删除专题成果
     */
    @PreAuthorize("@ss.hasPermi('archive:zt:achievement:remove')")
    @Log(title = "专题成果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{chengguoIds}")
    public AjaxResult remove(@PathVariable String[] chengguoIds) {
        return toAjax(yqZhuantiChengguoService.deleteYqZhuantiChengguoByChengguoIds(chengguoIds));
    }

    /**
     * 成果下载
     *
     * @param ids
     * @param response
     */
    @PreAuthorize("@ss.hasPermi('archive:zt:achievement:download')")
    @Log(title = "成果下载", businessType = BusinessType.EXPORT)
    @PostMapping("/download")
    public void download(@RequestParam("ids") String ids, HttpServletResponse response) throws Exception {
        yqZhuantiChengguoService.download(ids, response);
    }

    /**
     * 修改发布状态
     *
     * @param ids   成果id
     * @param state 状态
     * @return
     */
    @PreAuthorize("@ss.hasPermi('archive:zt:achievement:editState')")
    @Log(title = "修改发布状态", businessType = BusinessType.UPDATE)
    @PutMapping("/editState")
    public AjaxResult editState(@RequestParam("ids") String[] ids, @RequestParam("state") String state) {
        return toAjax(yqZhuantiChengguoService.updateIssueState(ids, state));
    }

}
